package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-mng-sys/models"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	var user models.User

	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}

	if user.Password != password {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "登录成功",
	})
	return

}

func List(c *gin.Context) {
	var data []models.User

	models.DB.Find(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": data,
	})
	return
}

func List1(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var data []models.User

	models.DB.Order("id desc").Offset(offset).Limit(size).Find(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": data,
	})
	return
}
